import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  // 首页路由
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      tbNum: 0,
      isTbShow: true
    }
  },
  // 登录路由
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  // 发货单路由
  {
    path: '/order',
    name: 'Order',
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue'),
    children: [
      {
        // 发货单的路由
        path: '/order/fahuodan',
        name: 'Fahuodan',
        component: () => import(/* webpackChunkName: "about" */ '../views/Fahuodan.vue'),
      },
      {
        // 提交评价的路由
        path: '/order/dingdanpingjia',
        name: 'DingDanPingJia',
        component: () => import(/* webpackChunkName: "about" */ '../views/DingDanPingJia.vue'),
      },
      {
        // 查看评价的路由
        path: '/order/chakanpingjia',
        name: 'ChaKanPingJia',
        component: () => import(/* webpackChunkName: "about" */ '../views/ChaKanPingJia.vue'),
      }
    ],
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    children: [
      { // 查看个人详细信息
        path: '/user/userxinxi',
        name: 'UserXinxi',
        component: () => import(/* webpackChunkName: "about" */ '../views/UserXinxi.vue'),
        meta: {
          tbNum: 2,
          isTbShow: true
        }
      },
      { // 查看当前版本
        path: '/user/userbanben',
        name: 'UserBanben',
        component: () => import(/* webpackChunkName: "about" */ '../views/UserBanben.vue'),
        meta: {
          tbNum: 2,
          isTbShow: true
        }
      }
    ],
    meta: {
      tbNum: 2,
      isTbShow: true
    }
  },
  // 消息中心路由
  {
    path: '/message',
    name: 'Message',
    component: () => import(/* webpackChunkName: "about" */ '../views/Message.vue'),
    children: [
      { // 查看个人详细信息
        path: '/message/messageinfo',
        name: 'MessageInfo',
        component: () => import(/* webpackChunkName: "about" */ '../views/MessageInfo.vue')
      }
    ],
  },
  // 物流单评价路由
  {
    // 查看评价的路由
    path: '/star',
    name: 'Star',
    component: () => import(/* webpackChunkName: "about" */ '../views/Star.vue'),
    children: [
      { // 评价
        path: '/star/starpingjia',
        name: 'StarPingjia',
        component: () => import(/* webpackChunkName: "about" */ '../views/StarPingjia.vue')
      },
      { // 查看评价
        path: '/star/starpjinfo',
        name: 'StarPjInfo',
        component: () => import(/* webpackChunkName: "about" */ '../views/StarPjInfo.vue')
      },
    ],
  },
  // 我的客户路由
  { // 客户列表
    path: '/cust',
    name: 'Cust',
    component: () => import(/* webpackChunkName: "about" */ '../views/Cust.vue'),
    children: [
      { // 客户视图界面
        path: '/cust/custwatch',
        name: 'CustWatch',
        component: () => import(/* webpackChunkName: "about" */ '../views/CustWatch.vue'),
        children: [
          { // 客户详细信息
            path: '/cust/custwatch/custinfo',
            name: 'CustInfo',
            component: () => import(/* webpackChunkName: "about" */ '../views/CustInfo.vue'),
            children: [
              { // 客户编辑详细信息
                path: '/cust/custwatch/custinfo/custchangeinfo',
                name: 'CustChangeInfo',
                component: () => import(/* webpackChunkName: "about" */ '../views/CustChangeInfo.vue')
              },
            ],
          },
        ],
      },
      { // 新增客户页面
        path: '/cust/custaddinfo',
        name: 'CustAddInfo',
        component: () => import(/*info webpackChunkName: "about" */ '../views/CustAddInfo.vue')
      },
    ],
  },
  // 客户关怀路由
  { // 走访客户页面
    path: '/visit',
    name: 'Visit',
    component: () => import(/*info webpackChunkName: "about" */ '../views/Visit.vue'),
    children: [
      { // 新增走访客户页面
        path: '/visit/visitadd',
        name: 'VisitAdd',
        component: () => import(/*info webpackChunkName: "about" */ '../views/VisitAdd.vue'),
        children: [
          { // 新增走访客户页面
            path: '/visit/visitadd/visitaddlist',
            name: 'VisitAddlist',
            component: () => import(/*info webpackChunkName: "about" */ '../views/VisitAddlist.vue')
          },
        ]
      },
      { // 新增走访客户页面详情
        path: '/visit/visitinfo',
        name: 'VisitInfo',
        component: () => import(/*info webpackChunkName: "about" */ '../views/VisitInfo.vue')
      },
    ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 书写路由拦截，当登录成功时跳转到首页，并确保用户已经登录
router.beforeEach((to, from, next) => {
  // 获取token,验证是否登录。
  let token = localStorage.getItem("token")
  if (to.path == "/home") {
    if (token) {
      // 跳转
      next()
    } else {
      // 没有token弹出提示并且跳转登录页。
      Vue.prototype.$toast('请先登录！')
      setTimeout(() => {
        next("/login")
      }, 1000)
    }
    // 截停函数，让后面的next调用不执行
    return
  }
  next()
})


export default router
